package com.sokal.easycare.modules.kpoint.service.activity;

import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.service.CrudService;
import com.sokal.easycare.common.utils.FastDFS.FastDFSClient;
import com.sokal.easycare.modules.kpoint.dao.activity.KPointActivityFileDao;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivity;
import com.sokal.easycare.modules.kpoint.entity.activity.KPointActivityFile;
import com.sokal.easycare.modules.product.entity.product.ProProductFile;

/**
 * 体验店活动附件Service
 * @author lihaipei
 * @version 2017-03-19
 */
@Service
@Transactional(readOnly = true)
public class KPointActivityFileService extends CrudService<KPointActivityFileDao, KPointActivityFile> {

	
	public KPointActivityFile get(String id) {
		KPointActivityFile kPointActivityFile = super.get(id);
		return kPointActivityFile;
	}
	
	public List<KPointActivityFile> findList(KPointActivityFile kPointActivityFile) {
		return super.findList(kPointActivityFile);
	}
	
	public Page<KPointActivityFile> findPage(Page<KPointActivityFile> page, KPointActivityFile kPointActivityFile) {
		return super.findPage(page, kPointActivityFile);
	}
	
	@Transactional(readOnly = false)
	public void save(KPointActivityFile kPointActivityFile) {
		super.save(kPointActivityFile);
	}
	
	@Transactional(readOnly = false)
	public void delete(KPointActivityFile kPointActivityFile) {
		super.delete(kPointActivityFile);
	}

	@Transactional(readOnly = false)
	public void saveFiles(MultipartFile activityFile, String activityId) {
		try {
			KPointActivity kPointActivity = new KPointActivity();
			kPointActivity.setId(activityId);
			
			KPointActivityFile kPointActivityFile = new KPointActivityFile();
			kPointActivityFile.setFileUrl(FastDFSClient.uploadFile(activityFile, activityFile.getOriginalFilename()));
			kPointActivityFile.setFileName(activityFile.getOriginalFilename());
			kPointActivityFile.setFileType(ProProductFile.FILE_TYPE_SINGLE_IMAGE);
			kPointActivityFile.setkPointActivity(kPointActivity);
			
			super.save(kPointActivityFile);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Transactional(readOnly = false)
	public void updateSort(KPointActivityFile kPointActivityFile) {
		dao.updateSort(kPointActivityFile);
	}
	
}